import mysql.connector
from dirbot import settings

MYSQL_HOSTS = settings.MYSQL_HOSTS
MYSQL_USER = settings.MYSQL_USER
MYSQL_PASSWORD = settings.MYSQL_PASSWORD
MYSQL_PORT = settings.MYSQL_PORT
MYSQL_DB = settings.MYSQL_DB

#连接mysql 初始化了一个MySQL的操作游标
cnx = mysql.connector.connect(user=MYSQL_USER, password=MYSQL_PASSWORD, host = MYSQL_HOSTS ,database=MYSQL_DB)
cur = cnx.cursor(buffered=True)


class Sql:

    @classmethod
    def insertKaolaRecoed(cls,productName,url,picUrl,price,singlePrice,tag,commentCount):
        sql = 'insert into t_kaola_product (product_name,product_url,img_url,price,single_price,' \
              'tag,comment_count) values (%(productName)s,%(url)s,%(picUrl)s,%(price)s,%(singlePrice)s,%(tag)s,%(commentCount)s)'
        values = {
                    'productName':productName,
                    'url':url,
                    'picUrl':picUrl,
                    'price':price,
                    'singlePrice':singlePrice,
                    'tag':tag,
                    'commentCount':commentCount
        }
        cur.execute(sql,values)
        cnx.commit()